#!/bin/env bash

USER=$(id -u)
GROUP=$(id -g)
DIR=$(pwd)

if [ ! -d $DIR/content ]; then
  while true; do
    read -p "Content not found. Want to initialize current directory? [yN] " yn
    case $yn in
      [Yy]*)
        break;;
      *)
        exit;;
    esac
  done
fi

mkdir -p $DIR/content $DIR/config $DIR/resources $DIR/public $DIR/static
touch $DIR/.hugo_build.lock

COMMON_ARGS="--rm -it --network=host --user $USER:$GROUP \
  -v /etc/passwd:/etc/passwd:ro \
  -v /etc/shadow:/etc/shadow:ro \
  -v $DIR/config:/opt/mdtool/site/config:ro \
  -v $DIR/content:/opt/mdtool/site/content \
  -v $DIR/resources:/opt/mdtool/site/resources \
  -v $DIR/public:/opt/mdtool/site/public \
  -v $DIR/static:/opt/mdtool/site/static \
  -v $DIR/.hugo_build.lock:/opt/mdtool/site/.hugo_build.lock"

docker run $COMMON_ARGS jayven/mdtool hugo $@
